VBA Do...While循环

您所在的位置:网站首页 c 教程 dowhile 循环 VBA Do...While循环

VBA Do...While循环

2023-10-29 06:37| 来源: 网络整理| 查看: 265

一个Do...while循环用于只要条件为真就重复一组语句。该条件可以在循环开始时或循环结束时检查。

语法

以下是VBA中的一个Do...While循环的语法。

Do While condition [statement 1] [statement 2] ... [statement n] [Exit Do] [statement 1] [statement 2] ... [statement n] Loop

流程图

示例

以下示例使用Do...while循环来检查循环开始处的条件。循环内部的语句只有在条件成立时才被执行。

Private Sub Constant_demo_Click() Do While i < 5 i = i + 1 msgbox "The value of i is : " & i Loop End Sub

当上面的代码被执行时,它会在消息框中输出下面的输出。

The value of i is : 1 The value of i is : 2 The value of i is : 3 The value of i is : 4 The value of i is : 5 备用/替代语法

另外还有一个替代语句for...while循环,用于在循环结束时检查条件。下面的例子解释了这两种语法的主要区别。语法 -

Do [statement 1] [statement 2] ... [statement n] [Exit Do] [statement 1] [statement 2] ... [statement n] Loop While condition 示例

以下示例使用Do...while循环来检查循环结束时的条件。循环内的语句至少执行一次,即使条件为False。

Private Sub Constant_demo_Click() i = 10 Do i = i + 1 MsgBox "The value of i is : " & i Loop While i < 3 'Condition is false.Hence loop is executed once. End Sub

当上面的代码被执行时,它会在消息框中输出下面的输出。

上一篇: VBA循环 下一篇: VBA字符串


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3